package com.hfjyz.registration.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.registration.command.AdminLoginCommand;
import com.hfjyz.registration.command.ManagerEditCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.service.ManagerService;

public class ManagerEditController extends SimpleFormController {

	private ManagerService managerService;

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ManagerEditCommand cmd = (ManagerEditCommand) command;
		AdminLoginCommand adminCmd = (AdminLoginCommand) request.getSession()
				.getAttribute("AdminInfo");
		if (cmd.getId() == null) {
			managerService.loggin(request, ExamContants.LOGING_TYPE_INFO,
					(Integer) null, ExamContants.LOGING_USER_TYPE_MANAGE,
					adminCmd.getAdminId(), ExamContants.LOGING_OP_TYPE_ADD,
					"管理员追加");
		} else {
			managerService.loggin(request, ExamContants.LOGING_TYPE_INFO,
					(Integer) null, ExamContants.LOGING_USER_TYPE_MANAGE,
					adminCmd.getAdminId(), ExamContants.LOGING_OP_TYPE_EDIT,
					"管理员编辑");
		}
		managerService.editOk(request, cmd, errors);
		if (errors.hasErrors()) {
			return new ModelAndView(getFormView(), errors.getModel());
		}
		return new ModelAndView(new RedirectView("managerList.action"),
				errors.getModel());
	}

	/**
	 * @return the departService
	 */
	public ManagerService getManagerService() {
		return managerService;
	}

	/**
	 * @param departService
	 *            the departService to set
	 */
	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}

}